<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="任务主题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subject">
              <a-input v-model="model.subject" placeholder="请输入任务主题" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="1 是 0 否" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detectionTarget">
              <a-input v-model="model.detectionTarget" placeholder="请输入1 是 0 否" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务目标" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="taskTarget">
              <a-textarea v-model="model.taskTarget" rows="4" placeholder="请输入任务目标" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="1、全部端口 2、指定端口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="portType">
              <a-input v-model="model.portType" placeholder="请输入1、全部端口 2、指定端口" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="检测方式 1、全部执行 2 部分执行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detectionWay">
              <a-input v-model="model.detectionWay" placeholder="请输入检测方式 1、全部执行 2 部分执行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="端口范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <a-input v-model="model.port" placeholder="请输入端口范围" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="扫描参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scanPara">
              <a-input v-model="model.scanPara" placeholder="请输入扫描参数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="扫描模式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scanModel">
              <a-input v-model="model.scanModel" placeholder="请输入扫描模式" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="登录信息" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="loginInfo">
              <a-textarea v-model="model.loginInfo" rows="4" placeholder="请输入登录信息" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="实例数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="instanceNum">
              <a-input v-model="model.instanceNum" placeholder="请输入实例数量" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="运行状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="runState">
              <a-input v-model="model.runState" placeholder="请输入运行状态" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="状态（-1：作废，0：审批中，1：审核通过）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-input-number v-model="model.state" placeholder="请输入状态（-1：作废，0：审批中，1：审核通过）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createAccount">
              <a-input v-model="model.createAccount" placeholder="请输入创建人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modifyTime">
              <j-date placeholder="请选择修改时间" v-model="model.modifyTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="netpt_job_ser" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="netptJobSerTable.loading"
          :columns="netptJobSerTable.columns"
          :dataSource="netptJobSerTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="netpt_job_detection" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="netptJobDetectionTable.loading"
          :columns="netptJobDetectionTable.columns"
          :dataSource="netptJobDetectionTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="netpt_job_iplist" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="netptJobIplistTable.loading"
          :columns="netptJobIplistTable.columns"
          :dataSource="netptJobIplistTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="netpt_job_ret" :key="refKeys[3]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[3]"
          :loading="netptJobRetTable.loading"
          :columns="netptJobRetTable.columns"
          :dataSource="netptJobRetTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'NetptJobForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['netptJobSer', 'netptJobDetection', 'netptJobIplist', 'netptJobRet', ],
        tableKeys:['netptJobSer', 'netptJobDetection', 'netptJobIplist', 'netptJobRet', ],
        activeKey: 'netptJobSer',
        // netpt_job_ser
        netptJobSerTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '任务id',
              key: 'jobId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'ip',
              key: 'ip',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '端口',
              key: 'port',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '服务',
              key: 'serve',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '版本',
              key: 'versions',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '领用时间',
              key: 'receiveTime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '领用状态 -1，0，1',
              key: 'receiveState',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '完成状态 0未完成 1已完成',
              key: 'finishState',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态（-1：作废，0：审批中，1：审核通过）',
              key: 'state',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createAccount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修改时间',
              key: 'modifyTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // netpt_job_detection
        netptJobDetectionTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '任务id',
              key: 'jobId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检测id',
              key: 'detectionId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '执行状态 1 执行 0 不执行',
              key: 'exceState',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态（-1：作废，0：审批中，1：审核通过）',
              key: 'state',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createAccount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修改时间',
              key: 'modifyTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // netpt_job_iplist
        netptJobIplistTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '任务id',
              key: 'jobId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'ip',
              key: 'ip',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '领用时间',
              key: 'receiveTime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '领用状态 -1，0，1',
              key: 'receiveState',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '完成状态 0未完成 1已完成',
              key: 'finishState',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态（-1：作废，0：审批中，1：审核通过）',
              key: 'state',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createAccount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修改时间',
              key: 'modifyTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // netpt_job_ret
        netptJobRetTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '任务id',
              key: 'jobId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检测id',
              key: 'detectionId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'ip',
              key: 'ip',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '端口',
              key: 'port',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '发送时间',
              key: 'sendTime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检测结果 1成功 0 失败',
              key: 'detectionResult',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'payload',
              key: 'payload',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '返回信息',
              key: 'returnInfo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态（-1：作废，0：审批中，1：审核通过）',
              key: 'state',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createAccount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修改时间',
              key: 'modifyTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/sermanager/netptJob/add",
          edit: "/sermanager/netptJob/edit",
          queryById: "/sermanager/netptJob/queryById",
          netptJobSer: {
            list: '/sermanager/netptJob/queryNetptJobSerByMainId'
          },
          netptJobDetection: {
            list: '/sermanager/netptJob/queryNetptJobDetectionByMainId'
          },
          netptJobIplist: {
            list: '/sermanager/netptJob/queryNetptJobIplistByMainId'
          },
          netptJobRet: {
            list: '/sermanager/netptJob/queryNetptJobRetByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.netptJobSerTable.dataSource=[]
        this.netptJobDetectionTable.dataSource=[]
        this.netptJobIplistTable.dataSource=[]
        this.netptJobRetTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.netptJobSer.list, params, this.netptJobSerTable)
          this.requestSubTableData(this.url.netptJobDetection.list, params, this.netptJobDetectionTable)
          this.requestSubTableData(this.url.netptJobIplist.list, params, this.netptJobIplistTable)
          this.requestSubTableData(this.url.netptJobRet.list, params, this.netptJobRetTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          netptJobSerList: allValues.tablesValue[0].values,
          netptJobDetectionList: allValues.tablesValue[1].values,
          netptJobIplistList: allValues.tablesValue[2].values,
          netptJobRetList: allValues.tablesValue[3].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>